using System;
using System.IO;
using System.Web;
using System.Xml;

namespace CodeStory.Core
{
	public static class CodeStoryConfig
	{
		static CodeStoryConfig() {
			Initialize();
		}

		private static void Initialize() {
			string configFileName = string.Empty;
			HttpContext context = HttpContext.Current;
			if (context != null) {
				configFileName = context.Server.MapPath("~/codestory.config");
			} else {
				configFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "codestory.config");
			}
			if (!File.Exists(configFileName)) {
				throw new FileNotFoundException("CodeStory's configuration file was not found!", configFileName);
			}
			XmlDocument doc = new XmlDocument();
			doc.Load(configFileName);
			LoadConfigFromXmlDocument(doc);
		}

		private static void LoadConfigFromXmlDocument(XmlDocument doc) {
			string _defaultSkinName = doc.DocumentElement.GetAttribute("defaultSkinName");
			if (!string.IsNullOrEmpty(_defaultSkinName)) {
				defaultSkinName = _defaultSkinName;
			}
			string _defaultPageTabName = doc.DocumentElement.GetAttribute("defaultPageTabName");
			if (!string.IsNullOrEmpty(_defaultPageTabName)) {
				defaultPageTabName = _defaultPageTabName;
			}

			string _defaultPortletTemplate = doc.DocumentElement.GetAttribute("defaultPortletTemplate");
			if (!string.IsNullOrEmpty(_defaultPortletTemplate)) {
				defaultPortletTemplate = _defaultPortletTemplate;
			}

			foreach (XmlNode node in doc.DocumentElement.ChildNodes) {
				if (node.NodeType != XmlNodeType.Element) {
					continue;
				}
				XmlElement curEl = (XmlElement)node;
				switch (curEl.LocalName) {
					default:
						break;
				}
			}
		}

		private static string defaultPortletTemplate = "";

		public static string DefaultPortletTemplate {
			get { return CodeStoryConfig.defaultPortletTemplate; }
			set { CodeStoryConfig.defaultPortletTemplate = value; }
		}

		private static string loginUrl = "";
		public static string LoginUrl {
			get { return loginUrl; }
			set { loginUrl = value; }
		}

		private static string defaultSkinName = "default";
		public static string DefaultSkinName {
			get { return defaultSkinName; }
			set { defaultSkinName = value; }
		}
		private static string defaultPageTabName = "CodeStory.Default";
		public static string DefaultPageTabName {
			get { return defaultPageTabName; }
			set { defaultPageTabName = value; }
		}
	}
}